<?php
 class actionlog extends MX_Controller {
	function actionlog(){
		parent::__construct();
		$this->load->model('actionlog_model');
	}
	// this controller is used to create log of user action
	function index(){
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
		$vars = array();
		$this->load->model('actionlog_model');
		$actionType = $this->actionlog_model->getActionType();
				
		$vars["typeList"] = $actionType;
		$content = $this->load->view('actionlog_view', $vars,true);
		Template::display($content);
	}
	function saveActionLog($action,$obejct_name,$object_id,$content,$email="",$parent_id=0){
		return $this->actionlog_model->saveActionLog($action,$obejct_name,$object_id,$content,$email,$parent_id);
	}
	
	function searchUserLog(){
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
	
		if(empty($this->session->userdata['is_logged_in']) || $this->session->userdata['is_logged_in']==false)
		{
			$SURL="Location: ".base_url()."login";
			header($SURL);
			exit();
		}
		$this->load->library('CommonUtils');
		$this->load->model('actionlog_model');
		$fromDate = $toDate = "";
		if($_POST['dateFrom']!= "0")
		{
			$fromDate=$this->public_model->convertToYMD($this->input->post('dateFrom'), "-");
			//$fromDate = $this->commonutils->changeFormatDate1($_POST['dateFrom']); 
		}
		if($_POST['dateTo']!= "0"){
			$toDate=$this->public_model->convertToYMD($this->input->post('dateTo'), "-");
			//$toDate = $this->commonutils->changeFormatDate1($_POST['dateTo']);
		}
		$page = $_POST['page'];
		//// validatition
		$errList = "";
		if($fromDate== " ")$errList .= "The From date field must contain a valid date.<br/>";
		if($toDate== " ")$errList .= "The To date field must contain a valid date.";
		if($errList=="" && $fromDate!="" && $toDate!=""){
			$dtFr = new DateTime($fromDate);
			$dtTo = new DateTime($toDate);
			if($dtTo<$dtFr)$errList .= "To date must equal or greater than From date.";
		}
		
		if($errList != ""){
			$vars = array("hasError"=>true,
						  "keysearch"=>$_POST['key'],
						  "frdate"=>$_POST['dateFrom'],
						  "todate"=>$_POST['dateTo'],
						  "email"=>$_POST['email'],
						  "actionType"=>$_POST['actionType'],
						  "errorList"=>$errList);
		}	
		else{
			$numProductPerPage = $this->public_model->getSystemParameterValue("itemsPerPage"); //$this->commonutils->getNumberProductPerPage();
			$limitPage = $this->commonutils->getNumberPageDisplay();
			$startIndex = ($page -1)*$numProductPerPage;
			$endIndex = $page*$numProductPerPage;
			
			$searchKey = $_POST['key'];
			$actionType = $_POST['actionType'];
			$email = $_POST['email'];		
					
			$logs = $this->actionlog_model->searchUserLog($searchKey, $actionType, $fromDate, $toDate, $email, $startIndex,$endIndex);
			$logList = array();
			
			foreach($logs as $log){
					$logList[]=$log;
			}
			$vars["logList"] = $logList;
			$vars["logList"] = $logList;
			
			$pPaging = array();
			$totalResult = count($this->actionlog_model->searchUserLogTotal($searchKey, $actionType, $fromDate, $toDate, $email));		
			
			$pPaging = $this->commonutils->paging($page,$numProductPerPage,$totalResult,$limitPage);
			$vars["pPaging"] = $pPaging;
		}	
		echo json_encode($vars);		
	}
	
	function del(){
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
		
		$listId=$this->input->post('listIds');
		$this->load->model('actionlog_model');
		$delWishList =  $this->actionlog_model->delete_logs($listId);
		echo "Pass"."|".base_url()."searchUserLog"; 
	}	
 }
 ?>